<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/24
 * Time: 13:57
 */

namespace app\common\model;

use think\Cache;
use XznsTools\cache\CacheManage;

/**
 * 订单模型
 * Class Orders
 * @package app\common\model
 */
class Orders extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_orders';

    protected $update = ['update_time'];

    private $acg_gids;
    private $day_gids;
    private $month_gids;
    private $year_gids;
    private $fortune_gids;
    private $video_gids;
    private $active_start_time;
    private $active_end_time;

    const TIME_NUM = 'order:time_num';//一段时间的所有订单数量
    const REC_TIME_NUM = 'order:rec:time_num';//一段时间的校准生辰订单数量
    const FORTUNE_TIME_NUM = 'order:fortune:time_num:';//一段时间的运势订单数量(日运,月运,年运)
    const MAP_TIME_NUM = 'order:map:time_num';//一段时间的地图订单数量
    const VIDEO_TIME_NUM = 'order:video:time_num';//一段时间的课程订单数量
    const NUM_FOR_COURSE_ORDER = 'order:course:total_num:';//课程购买人数缓存键名
    const NUM_FOR_FORTUNE_ORDER = 'fortune_orders_people_total';//运势购买人数缓存键名
    const FORTUNE_ORDER_BASE_NUM = 'fortune_order_base_num';//运势购买人数后台基数缓存键名
    const ALL_REC_ORDER = 'all_rec_orders_';//用户所有校准生辰订单缓存键名
    const ALL_FORTUNE_ORDER = 'all_fortune_orders_';//用户所有运势订单缓存键名
    const ALL_MAP_ORDER = 'all_acg_map_orders_';//用户所有幸运之城订单缓存键名
    const ALL_VIDEO_ORDER = 'all:video:orders_';//用户所有教学视频订单缓存键名
    const REC_ORDERS_PAY = 'rec_orders_pay_';//当前档案已购买的校准生辰缓存键名
    const FORTUNE_ORDERS_PAY = '_fortune_orders_pay_';//当前档案已购买的日月年运势缓存键名
    const MAP_ORDERS_PAY = 'acg_map_orders_pay_';//当前档案已购买的幸运之城缓存键名
    const VIDEO_ORDERS_PAY = 'video:orders:pay:';//当前用户已购买的视频缓存键名
    const VIDEO_ORDERS_UNPAID = 'video:orders:unpaid:';//当前用户未购买的视频缓存键名
    const ORDER_INFO_ID = 'order:info:';//订单详情缓存键名,根据订单id
    const CACHE_TIME = 86400 * 2;

    protected function initialize()
    {
        parent::initialize();
        $goodsConfig = config('goods');
        $activeConfig = config('active.active_time');
        $this->acg_gids = $goodsConfig['acg_id'];
        $this->day_gids = $goodsConfig['day_id'];
        $this->month_gids = $goodsConfig['month_id'];
        $this->year_gids = $goodsConfig['year_id'];
        $this->fortune_gids = $goodsConfig['fortune_id'];
        $this->video_gids = $goodsConfig['video_id'];
        $this->active_start_time = strtotime($activeConfig['start']);
        $this->active_end_time = strtotime($activeConfig['end']);
    }

    /**
     * 设置器,updatetime默认存入时间戳
     * @return int
     */
    public function setUpdateTimeAttr(): int
    {
        return time();
    }

    /**
     * 获取当前用户的所有订单
     * @param array $map
     * @return array
     */
    public function getUidOrders($map = [], $map_queue = 1): array
    {
        $uid = $map['uid'];
        $type = $map['type'];
        $is_del = $map['is_del'];

        $rec_orders = $this->getUidRecOrder(['uid' => $uid, 'is_del' => $is_del]);
        $fortune_orders = $this->getUidFortuneOrder(['uid' => $uid, 'is_del' => $is_del]);
        $acg_orders = $this->getUidAcgOrder(['uid' => $uid, 'is_del' => $is_del]);
        $video_orders = $this->getUidVideoOrder(['uid' => $uid, 'is_del' => $is_del]);
        $new_rec_orders = [];
        $new_fortune_orders = [];
        $new_acg_orders = [];
        $new_video_orders = [];
        switch ($type)
        {
            case 0:
                $new_rec_orders = $rec_orders ?: [];
                $new_fortune_orders = $fortune_orders ?: [];
                $new_acg_orders = $acg_orders ?: [];
                $new_video_orders = $video_orders ?: [];
                break;
            case 1:
                if ($rec_orders)
                {
                    foreach ($rec_orders as $v)
                    {
                        if ($v['restatus'] == 0)
                        {
                            $new_rec_orders[] = $v;
                        }
                    }
                }
                if ($fortune_orders)
                {
                    foreach ($fortune_orders as $v)
                    {
                        if ($v['status'] == 0)
                        {
                            $new_fortune_orders[] = $v;
                        }
                    }
                }
                if ($acg_orders)
                {
                    foreach ($acg_orders as $v)
                    {
                        if ($v['status'] == 0)
                        {
                            $new_acg_orders[] = $v;
                        }
                    }
                }
                if ($video_orders)
                {
                    foreach ($video_orders as $v)
                    {
                        if ($v['status'] == 0)
                        {
                            $new_video_orders[] = $v;
                        }
                    }
                }
                break;
            case 2:
                if ($rec_orders)
                {
                    foreach ($rec_orders as $v)
                    {
                        if ($v['restatus'] == 1)
                        {
                            $new_rec_orders[] = $v;
                        }
                    }
                }
                if ($fortune_orders)
                {
                    foreach ($fortune_orders as $v)
                    {
                        if ($v['status'] == 1 && ($v['type'] == 2 || $v['type'] == 3))
                        {
                            $new_fortune_orders[] = $v;
                        }
                    }
                }
                if ($map_queue > 1 && $acg_orders)
                {
                    foreach ($acg_orders as $v)
                    {
                        if ($v['status'] == 1)
                        {
                            $new_fortune_orders[] = $v;
                        }
                    }
                }
                break;
            case 3:
                if ($rec_orders)
                {
                    foreach ($rec_orders as $v)
                    {
                        if ($v['restatus'] > 1)
                        {
                            $new_rec_orders[] = $v;
                        }
                    }
                }
                if ($fortune_orders)
                {
                    foreach ($fortune_orders as $v)
                    {
                        if ($v['status'] >= 1 && $v['type'] == 1)
                        {
                            $new_fortune_orders[] = $v;
                        } elseif ($v['status'] > 1 && ($v['type'] == 2 || $v['type'] == 3))
                        {
                            $new_fortune_orders[] = $v;
                        }
                    }
                }
                //老版本幸运之城状态只有购买成功
                if ($acg_orders && $map_queue == 1)
                {
                    foreach ($acg_orders as $v)
                    {
                        if ($v['status'] >= 1)
                        {
                            $new_acg_orders[] = $v;
                        }
                    }
                }
                //新版本幸运之城有计算中
                if ($acg_orders && $map_queue > 1)
                {
                    foreach ($acg_orders as $v)
                    {
                        if ($v['status'] > 1)
                        {
                            $new_acg_orders[] = $v;
                        }
                    }
                }
                if ($video_orders)
                {
                    foreach ($video_orders as $v)
                    {
                        if ($v['status'] >= 1)
                        {
                            $new_video_orders[] = $v;
                        }
                    }
                }
                break;
        }

        //合并数据
        $orders = array_merge($new_rec_orders, $new_fortune_orders, $new_acg_orders, $new_video_orders);

        return $orders;
    }

    /**
     * 获取当前用户所有校准生辰订单id
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getUidRecOrder($map = [])
    {
        $uid = $map['uid'];
        $is_del = $map['is_del'];

        $user_rec_order_id = false;

        if ($this->cache_on >= 1)
        {
            $user_rec_order_id = Cache::get(self::ALL_REC_ORDER . $uid);
        }

        // 校准生辰订单id
        if ($user_rec_order_id === false)
        {
            try
            {
                $where = ['uid' => $uid, 'is_del' => $is_del, 'gid' => ['in', config('goods.rec_id')]];
                $field = 'id';
                $orders = $this
                    ->field($field)
                    ->where($where)
                    ->order('create_time', 'DESC')
                    ->select();

                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_rec_order_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_rec_order_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::ALL_REC_ORDER . $uid, $user_rec_order_id, self::CACHE_TIME);
                }
            }
            catch (\Exception $e)
            {
                $user_rec_order_id = '';// 文章列表默认为空
            }
        }

        // 将订单id转为数组
        $user_rec_order_id = explode(',', $user_rec_order_id);

        // 去除数组最后一个元素
        array_pop($user_rec_order_id);

        $data = $this->getOrdersDetail(['oids' => $user_rec_order_id, 'order_type' => 1]);

        return $data;

    }

    /**
     * 获取当前用户所有运势订单id
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function getUidFortuneOrder($map = [])
    {
        $uid = $map['uid'];
        $is_del = $map['is_del'];

        $user_fortune_order_id = false;

        if ($this->cache_on >= 1)
        {
            $user_fortune_order_id = Cache::get(self::ALL_FORTUNE_ORDER . $uid);
        }
        //运势订单id
        if ($user_fortune_order_id === false)
        {
            try
            {
                $where = ['uid' => $uid, 'is_del' => $is_del, 'gid' => ['in', $this->fortune_gids]];
                $orders = $this
                    ->field('id')
                    ->where($where)
                    ->order('create_time', 'DESC')
                    ->select();

                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_fortune_order_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_fortune_order_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::ALL_FORTUNE_ORDER . $uid, $user_fortune_order_id, self::CACHE_TIME);
                }
            }
            catch (\Exception $e)
            {
                $user_fortune_order_id = '';// 文章列表默认为空
            }
        }
        // 将订单id转为数组
        $user_fortune_order_id = explode(',', $user_fortune_order_id);

        // 去除数组最后一个元素
        array_pop($user_fortune_order_id);

        $data = $this->getOrdersDetail(['oids' => $user_fortune_order_id, 'order_type' => 2]);

        return $data;

    }

    /**
     * 获取当前用户所有命格线订单id
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function getUidAcgOrder($map = [])
    {
        $uid = $map['uid'];
        $is_del = $map['is_del'];

        $user_acg_order_id = false;

        if ($this->cache_on >= 1)
        {
            $user_acg_order_id = Cache::get(self::ALL_MAP_ORDER . $uid);
        }

        //运势订单id
        if ($user_acg_order_id === false)
        {
            try
            {
                $where = ['or.uid' => $uid, 'or.is_del' => $is_del, 'og.gid' => ['in', $this->acg_gids]];
                $orders = $this
                    ->alias('or')
                    ->field('or.id')
                    ->join('app_orders_goods og', 'or.id=og.oid', 'LEFT')
                    ->join('app_goods g', 'og.gid=g.id', 'LEFT')
                    ->where($where)
                    ->order('or.create_time', 'DESC')
                    ->group('or.order_id')
                    ->select();

                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_acg_order_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_acg_order_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::ALL_MAP_ORDER . $uid, $user_acg_order_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_acg_order_id = '';// 文章列表默认为空
            }

        }

        // 将订单id转为数组
        $user_acg_order_id = explode(',', $user_acg_order_id);

        // 去除数组最后一个元素
        array_pop($user_acg_order_id);

        $data = $this->getOrdersDetail(['oids' => $user_acg_order_id, 'order_type' => 3]);

        return $data;
    }

    /**
     * 获取当前用户所有命格线订单id
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function getUidVideoOrder($map = [])
    {
        $uid = $map['uid'];
        $is_del = $map['is_del'] ?? 0;

        $user_order_id = false;


        $cache_name = self::ALL_VIDEO_ORDER;
        $gids = $this->video_gids;

        if ($this->cache_on >= 1)
        {
            $user_order_id = Cache::get($cache_name . $uid);
        }

        //运势订单id
        if ($user_order_id === false)
        {
            try
            {
                $where = ['or.uid' => $uid, 'or.is_del' => $is_del, 'or.gid' => ['in', $gids]];
                $orders = $this
                    ->alias('or')
                    ->field('or.id')
                    ->join('app_goods g', 'or.gid=g.id', 'LEFT')
                    ->where($where)
                    ->order('or.create_time', 'DESC')
                    ->group('or.order_id')
                    ->select();

                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_order_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_order_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set($cache_name . $uid, $user_order_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_order_id = '';// 文章列表默认为空
            }

        }

        // 将订单id转为数组
        $user_order_id = explode(',', $user_order_id);

        // 去除数组最后一个元素
        array_pop($user_order_id);

        return $this->getOrdersDetail(['oids' => $user_order_id, 'order_type' => 4]);
    }

    /**
     * 获取当前档案已支付的校准生辰订单id
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function rec_orders($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];

        $user_rec_pay_id = false;

        if ($this->cache_on >= 1)
        {
            $user_rec_pay_id = Cache::get(self::REC_ORDERS_PAY . $rid);
        }

        // 校准生辰订单id
        if ($user_rec_pay_id === false)
        {
            try
            {
                $where = ['re.rid' => $rid, 'o.uid' => $uid, 're.status' => ['in', [1, 2, 3]]];
                $orders = $this
                    ->alias('o')
                    ->field('o.id')
                    ->join('wechat_backup_07_18_14_43_00.app_rectification re', 'o.reid=re.id', 'LEFT')
                    ->where($where)
                    ->order('o.create_time', 'DESC')
                    ->select();
                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_rec_pay_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_rec_pay_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::REC_ORDERS_PAY . $rid, $user_rec_pay_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_rec_pay_id = '';// 文章列表默认为空
            }
        }

        // 将订单id转为数组
        $user_rec_pay_id = explode(',', $user_rec_pay_id);

        // 去除数组最后一个元素
        array_pop($user_rec_pay_id);

        $data = $this->getOrdersDetail(['oids' => $user_rec_pay_id, 'order_type' => 1]);

        return $data;

    }

    /**
     * 获取当前档案已支付的运势订单,日,月,年分开
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function fortune_orders($map = [])
    {
        $uid = (int)$map['uid'];
        $rid = (string)$map['rid'];
        $type = $map['type'];

        $user_fortune_pay_id = false;

        if ($this->cache_on >= 1)
        {
            // 按当前类别获取已购买的订单ID缓存
            $user_fortune_pay_id = Cache::get($rid . self::FORTUNE_ORDERS_PAY . $type);
        }

        // 运势订单id
        if ($user_fortune_pay_id === false)
        {
            switch ($type)
            {
                case 1:
//                    $where = ['fr.rids' => ['like', "%{$rid}%"], 'o.uid' => $uid, 'o.status' => 1, 'o.gid' => ['in', config('goods.day_id')]];
                    $where = ['o.uid' => $uid, 'o.status' => ['in', [1, 4]], 'o.gid' => ['in', config('goods.day_id')]];
                    break;
                case 2:
//                    $where = ['fr.rids' => ['like', "%{$rid}%"], 'o.uid' => $uid, 'o.status' => ['in', [1, 4]], 'o.gid' => ['in', config('goods.month_id')]];
                    $where = ['o.uid' => $uid, 'o.status' => ['in', [1, 4]], 'o.gid' => ['in', config('goods.month_id')]];
                    break;
                case 3:
//                    $where = ['fr.rids' => ['like', "%{$rid}%"], 'o.uid' => $uid, 'o.status' => ['in', [1, 4]], 'o.gid' => ['in', config('goods.year_id')]];
                    $where = ['o.uid' => $uid, 'o.status' => ['in', [1, 4]], 'o.gid' => ['in', config('goods.year_id')]];
                    break;
                default:
                    $where = ' 1 > 2';
                    break;
            }
            $where1 = "find_in_set($rid,f.rids)";

            try
            {
                $orders = $this->alias('o')
                    ->field('o.id')
                    ->join('app_goods g', 'o.gid=g.id')
                    ->join('wechat_backup_07_18_14_43_00.app_fortune f', 'o.fid=f.id')
                    ->where($where)
                    ->where($where1)
                    ->order('o.create_time', 'DESC')
                    ->selectOrFail();
                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_fortune_pay_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_fortune_pay_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set($rid . self::FORTUNE_ORDERS_PAY . $type, $user_fortune_pay_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_fortune_pay_id = '';// 文章列表默认为空
            }
        }

        // 将订单id转为数组
        $user_fortune_pay_id = explode(',', $user_fortune_pay_id);

        // 去除数组最后一个元素
        array_pop($user_fortune_pay_id);

        $data = $this->getOrdersDetail(['oids' => $user_fortune_pay_id, 'order_type' => 2]);

        return $data;
    }

    /**
     * 获取当前档案已支付的命格线订单
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function acg_map_orders($map = [])
    {
        $uid = $map['uid'];
        $rid = $map['rid'];

        $user_acg_pay_id = false;

        if ($this->cache_on >= 1)
        {
            $user_acg_pay_id = Cache::get(self::MAP_ORDERS_PAY . $rid);
        }

        // 校准生辰订单id
        if ($user_acg_pay_id === false)
        {
            try
            {
                $where = ['o.uid' => $uid, 'og.rid' => $rid, 'o.status' => ['in', [1, 4]]];
                $orders = $this
                    ->alias('o')
                    ->field('o.id')
                    ->join('wechat_backup_07_18_14_43_00.app_orders_goods og', 'o.id=og.oid')
                    ->where($where)
                    ->order('o.create_time', 'DESC')
                    ->select();

                $orders = json_decode(json_encode($orders, true), true);

                // 将订单id组成字符串
                if (!empty($orders))
                {
                    foreach ($orders as $acl)
                    {
                        $user_acg_pay_id .= $acl['id'] . ',';
                    }
                } else
                {
                    $user_acg_pay_id = '';
                }

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::MAP_ORDERS_PAY . $rid, $user_acg_pay_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_acg_pay_id = '';// 文章列表默认为空
            }
        }

        // 将订单id转为数组
        $user_acg_pay_id = explode(',', $user_acg_pay_id);

        // 去除数组最后一个元素
        array_pop($user_acg_pay_id);

        $data = $this->getOrdersDetail(['oids' => $user_acg_pay_id, 'order_type' => 3]);


        return $data;
    }

    /**
     * 获取当前用户已支付的视频订单
     * @param array $map
     * @return array|mixed|\PDOStatement|string|\think\Model
     */
    public function video_orders($map = [])
    {
        $uid = $map['uid'];

        $user_video_pay_id = false;

        if ($this->cache_on >= 1)
        {
            $user_video_pay_id = Cache::get(self::VIDEO_ORDERS_PAY . $uid);
        }

        // 教学视频订单id
        if ($user_video_pay_id === false)
        {
            try
            {
                $where = ['uid' => $uid, 'status' => ['in', [1, 4]], 'gid' => ['in', $this->video_gids]];
                $user_video_pay_id = $this
                    ->where($where)
                    ->order('create_time', 'DESC')
                    ->column('gid');

                $user_video_pay_id = json_decode(json_encode($user_video_pay_id, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::VIDEO_ORDERS_PAY . $uid, $user_video_pay_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_video_pay_id = [];// 文章列表默认为空
            }
        }


        return $user_video_pay_id;
    }

    /**
     * 获取视频未支付的订单
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function video_unpaid_orders($map = [])
    {
        $uid = $map['uid'];

        $user_video_pay_id = false;

        if ($this->cache_on >= 1)
        {
            $user_video_pay_id = Cache::get(self::VIDEO_ORDERS_UNPAID . $uid);
        }

        //  教学视频订单id
        if ($user_video_pay_id === false)
        {
            try
            {
                $where = ['uid' => $uid, 'status' => 0, 'gid' => ['in', $this->video_gids]];
                $user_video_pay_id = $this
                    ->field('id,gid,uid,order_id')
                    ->where($where)
                    ->order('create_time', 'DESC')
                    ->selectOrFail();

                $user_video_pay_id = json_decode(json_encode($user_video_pay_id, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::VIDEO_ORDERS_UNPAID . $uid, $user_video_pay_id, self::CACHE_TIME);
                }

            }
            catch (\Exception $e)
            {
                $user_video_pay_id = [];// 文章列表默认为空
            }
        }


        return $user_video_pay_id;
    }

    /**
     * 获取订单详情信息
     * @param array $map
     * @return array
     */
    public function getOrdersDetail($map = []): array
    {
        $oids = $map['oids'] ?? '';
        $order_ids = $map['order_id'] ?? '';
        $order_type = $map['order_type'];

        $data = [];

        if (!empty($oids) && $oids)
        {
            $ids = $oids;
            $whereField = 'or.id';

        } else
        {
            $ids = $order_ids;
            $whereField = 'or.order_id';
        }

        // 根据当前页订单id获取所需的订单信息
        if (!empty($ids) && $ids)
        {
            $ac_ids = [];
            $temp = [];

            // 从缓存订单内容,根据订单id
            foreach ($ids as $ac_id)
            {
                if ($this->cache_on >= 1)
                {
                    $temp[$ac_id] = Cache::get(self::ORDER_INFO_ID . $ac_id);
                } else
                {
                    $temp[$ac_id] = false;
                }

                // 订单内容未缓存，等待从数据库中获取
                if ($temp[$ac_id] === false)
                {
                    $ac_ids[] = $ac_id;
                    unset($temp[$ac_id]);
                }
            }

            // 从数据库获取订单内容
            if (!empty($ac_ids))
            {
                $where = [$whereField => ['in', $ac_ids]];
                try
                {
                    switch ($order_type)
                    {
                        //校准生辰订单详情
                        case 1:
                            $field = 'or.gid,g.name,g.or_prize,g.now_prize,g.expire_time,or.id oid,or.status,or.order_id,or.uid,or.trade_type,or.total_amount,or.order_type,or.create_time,or.pay_time,or.return_time,or.reid,re.status restatus,re.start_time,re.end_time,or.account,or.extend';
                            $orders = $this->alias('or')
                                ->field($field)
                                ->join('wechat_backup_07_18_14_43_00.app_goods g', 'or.gid=g.id')
                                ->join('wechat_backup_07_18_14_43_00.app_rectification re', 'or.reid=re.id')
                                ->where($where)
                                ->order('or.create_time', 'DESC')
                                ->selectOrFail();
                            break;
                        //运势订单详情
                        case 2:
                            $field = 'or.gid,or.reid,g.name,g.or_prize,g.now_prize,g.expire_time,or.id oid,or.status,or.fid,or.order_id,or.uid,or.order_type,or.trade_type,or.total_amount,or.create_time,or.pay_time,or.update_time,or.return_time,or.extend,fr.extend,fr.time,fr.rids,fr.type,or.account';
                            $orders = $this->alias('or')
                                ->field($field)
                                ->join('wechat_backup_07_18_14_43_00.app_goods g', 'or.gid=g.id')
                                ->join('wechat_backup_07_18_14_43_00.app_fortune fr', 'or.fid=fr.id')
                                ->where($where)
                                ->order('or.create_time', 'DESC')
                                ->selectOrFail();
                            break;
                        //幸运之城订单详情
                        case 3:
                            $field = 'og.gid,or.reid,g.name,g.or_prize,g.now_prize,g.expire_time,or.id oid,or.status,or.order_id,or.uid,or.order_type,or.trade_type,or.total_amount,or.create_time,or.pay_time,or.return_time,og.rid,or.account,or.extend';
                            $orders = $this->alias('or')
                                ->field($field)
                                ->join('wechat_backup_07_18_14_43_00.app_orders_goods og', 'or.id=og.oid')
                                ->join('wechat_backup_07_18_14_43_00.app_goods g', 'og.gid=g.id')
                                ->where($where)
                                ->order('or.create_time', 'DESC')
                                ->selectOrFail();
                            break;
                        //教学视频订单详情
                        case 4:
                            $field = 'c.id cid,c.title,or.gid,g.name,g.or_prize,g.now_prize,or.id oid,or.status,or.order_id,or.order_type,or.uid,or.trade_type,or.total_amount,or.create_time,or.pay_time,or.return_time,or.reid,or.account,or.order_type,or.extend';
                            $orders = $this->alias('or')
                                ->field($field)
                                ->join('wechat_backup_07_18_14_43_00.app_goods g', 'or.gid=g.id')
                                ->join('wechat_backup_07_18_14_43_00.zhunle_course c', 'or.gid=c.gid')
                                ->where($where)
                                ->order('or.create_time', 'DESC')
                                ->selectOrFail();
                            break;
                        default:
                            $orders = [];
                            break;
                    }

                    $orders = json_decode(json_encode($orders, true), true);
                }
                catch (\Exception $e)
                {
                    \think\Log::write('获取订单详情信息失败:' . $e->getMessage(), 'log', true);
                    $orders = [];
                }

                if (!empty($orders))
                {
                    foreach ($orders as $c)
                    {
                        // 将订单内容缓存
                        if ($this->cache_on >= 1)
                        {
                            Cache::set(self::ORDER_INFO_ID . $c['oid'], $c, self::CACHE_TIME);
                            Cache::set(self::ORDER_INFO_ID . $c['order_id'], $c, self::CACHE_TIME);
                        }
                        // 将数据库查出来的内容 写入到数组中为空的部分
                        $temp[$c['oid']] = $c;
                    }
                }
            }
            foreach ($temp as $k => $d)
            {
                if ($d)
                {
                    $data[] = $d;
                }
            }

        }
        return $data;
    }

    /**
     * 删除订单缓存
     * @param array $map
     * @return bool
     */
    public function rm_cache($map = []): bool
    {
        $uid = $map['uid'];
        $del_add = $map['del_add']; //0=>修改订单,1=>新增订单
        $order_id = $map['order_id'] ?? '';
        $oid = $map['oid'] ?? 0;

        if ($order_id)
        {
            $where['or.order_id'] = $order_id;
            $num = substr($order_id, 1, 1);
        } else
        {
            $where['or.id'] = $oid;
            $num = 0;
        }

        $data = [];

        if ($del_add == 0)
        {
            //获取档案信息
            try
            {
                switch ($num)
                {
                    //校准生辰
                    case 1:
                        $field = 're.rid';
                        $data = $this->alias('or')
                            ->field($field)
                            ->join('wechat_backup_07_18_14_43_00.app_rectification re', 'or.reid=re.id')
                            ->where($where)
                            ->findOrFail();
                        break;
                    //运势
                    case 2:
                        $field = 'fr.rids rid,fr.type ftype';
                        $data = $this->alias('or')
                            ->field($field)
                            ->join('wechat_backup_07_18_14_43_00.app_fortune fr', 'or.fid=fr.id')
                            ->where($where)
                            ->selectOrFail();
                        break;
                    //幸运之城
                    case 3:
                        $field = 'og.rid';
                        $data = $this->alias('or')
                            ->field($field)
                            ->join('wechat_backup_07_18_14_43_00.app_orders_goods og', 'or.id=og.oid')
                            ->where($where)
                            ->findOrFail();
                        break;
                    //教学视频
                    case 4:
                        $field = 'uid';
                        $data = $this
                            ->alias('or')
                            ->field($field)
                            ->where($where)
                            ->findOrFail();
                        break;
                    default:
                        $data = [];
                        break;
                }

                $data = json_decode(json_encode($data, true), true);

            }
            catch (\Exception $e)
            {
                $this->error = '订单缓存亲清除失败';

                return false;
            }
        }

        //删除或新增
        if (empty($data))
        {
            switch ($num)
            {
                case 1:
                    Cache::rm(self::ALL_REC_ORDER . $uid);
                    break;
                case 2:
                    Cache::rm(self::ALL_FORTUNE_ORDER . $uid);
                    break;
                case 3:
                    //当前用户所有幸运之城订单
                    Cache::rm(self::ALL_MAP_ORDER . $uid);
                    break;
                case 4:
                    //删除用户所有教学视频订单缓存
                    Cache::rm(self::ALL_VIDEO_ORDER . $uid);
                    break;
            }
        } //修改
        else
        {

            Cache::rm(self::ORDER_INFO_ID . $oid);
            Cache::rm(self::ORDER_INFO_ID . $order_id);

            switch ($num)
            {
                case 1:
                    Cache::rm(self::REC_ORDERS_PAY . $data['rid']);
                    break;
                case 2:
                    foreach ($data as $itme)
                    {
                        $rid = explode(',', $itme['rid']);
                        foreach ($rid as $item)
                        {
                            Cache::rm($item . self::FORTUNE_ORDERS_PAY . $itme['ftype']);
                        }
                    }
                    //运势人数自增1
                    Cache::inc(self::NUM_FOR_FORTUNE_ORDER);
                    break;
                case 3:
                    // 当前档案acg订单(已支付)
                    Cache::rm(self::MAP_ORDERS_PAY . $data['rid']);
                    //当前档案已解锁和中奖的幸运之城(用于幸运之城首页的解锁判断)
                    Cache::rm('recode_lines_' . $data['rid']);
                    break;
                case 4:
                    //删除用户已购买的教学视频订单缓存
                    Cache::rm(self::VIDEO_ORDERS_PAY . $uid);
                    //删除用户所有未支付教学订单缓存
                    Cache::rm(self::VIDEO_ORDERS_UNPAID . $uid);
                    //删除课程已购买人数
                    if (isset($map['gid']))
                    {
                        Cache::inc(self::NUM_FOR_COURSE_ORDER . $map['gid']);
                    }
                    break;
            }
        }

        return true;
    }

    /**
     * 获取运势解锁人数基数
     * @param array $map
     * @return int
     */
    public function fortune_base($map = []): int
    {
        $type = $map['type'];
        $data = false;
        //获取运势基数
        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::FORTUNE_ORDER_BASE_NUM);
        }

        if ($data === false)
        {
            try
            {
                $data = $this->table('wechatadmin.system_system')->field('value')->where('id', 'in', [2, 3])->select();

                $data = json_decode(json_encode($data, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::FORTUNE_ORDER_BASE_NUM, $data, self::CACHE_TIME);
                }
            }
            catch (\Exception $e)
            {
                $data = [];
            }
        }

        $arr = 0;
        if ($data)
        {
            switch ($type)
            {
                case 1:
                    $arr = $data[0]['value'];
                    break;
                case 2:
                    $arr = $data[1]['value'];
                    break;
            }
        }

        //统计订单购买人数
        $num = false;
        if ($this->cache_on >= 1)
        {
            $num = Cache::get(self::NUM_FOR_FORTUNE_ORDER);
        }
        if ($num === false)
        {
            try
            {
                $num = $this->where(['status' => ['in', [1, 4]]])->group('uid')->count('uid');

                if ($this->cache_on >= 1)
                {
                    Cache::set(self::NUM_FOR_FORTUNE_ORDER, $num, self::CACHE_TIME);
                }
            }
            catch (\Exception $e)
            {
                $num = 0;
            }
        }

        return $num + $arr;
    }

    /**
     * 获取精简版月运解锁数量
     * @return int
     * @throws \Exception
     */
    public function getSimFortuneOrderNum(): int
    {
        $unlockNum = CacheManage::getInstance()->setCacheConfig('fortune.sim_fortune_month_unlock_num')->getDataAndSet(function ()
        {
            $baseNum = $this->table('wechatadmin.system_system')->where('id', 2)->value('value');

            $unlockNum = $this->where(['order_type' => 12, 'status' => ['in', [1, 4]]])->count();

            return $baseNum - 90000 + $unlockNum;
        });

        return (int)$unlockNum;
    }

    /**
     * 在支付成功后,订单数自增
     * @param int $order_type
     * @param int $fortune_type
     */
    public function inc_order_num($order_type = 0, $fortune_type = 0): void
    {
        switch ($order_type)
        {
            case 1:
                Cache::inc(self::REC_TIME_NUM);
                break;
            case 2:
                Cache::inc(self::FORTUNE_TIME_NUM . $fortune_type);
                break;
            case 3:
                Cache::inc(self::MAP_TIME_NUM);
                break;
            case 4:
                Cache::inc(self::VIDEO_TIME_NUM);
                break;
            default:
                break;
        }

        if ($fortune_type === 12)
        {
            CacheManage::getInstance()->redisHandler->incr('fortune:month:sim:unlock_num');
        }
    }
}
